package com.yuan.java.scirpt;

import org.junit.Test;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

/**
 * Created by 袁恩光 on 2017/5/26.
 */
public class ScriptTest {

    @Test
    public void test() throws Exception {
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript");
        String script = "function sum(a,b){return a + b;} function test(){return 11111}";
        scriptEngine.eval(script);
        Invocable invocable = (Invocable) scriptEngine;
        Object result = invocable.invokeFunction("sum", 2, 2);
        Object result2 = invocable.invokeFunction("test", 2, 2);
        System.out.println(result + "||" + result2);
    }
}
